using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * 
 * Autor      : Jose Jornando de Carvalho Junior    
 * Data       : 10/04/2010 07:07:05
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace SERIX
{
   public class CIndicadoresBase
   {
       private string IDT_IND ;
       private string IDT_IND_BASE ;

	   /// <sumary>
       /// Construtor da classe indicadores_base
       /// <sumary>
       public CIndicadoresBase()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Indicadores_base
       /// <sumary>
       public CIndicadoresBase( string IDT_IND, string IDT_IND_BASE )
       {
          this.IDT_IND = IDT_IND;
          this.IDT_IND_BASE = IDT_IND_BASE;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// Identificador do indicador
        /// <sumary>
        public string Idt_ind 
		{
		   get
		   {
		       return this.IDT_IND;
		   }
		   set
		   {
		       this.IDT_IND = value;
		   }
		}
     	/// <sumary>
        /// Identificador do indicador base
        /// <sumary>
        public string Idt_ind_base 
		{
		   get
		   {
		       return this.IDT_IND_BASE;
		   }
		   set
		   {
		       this.IDT_IND_BASE = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Indicadores_base.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SRX_INDICADORES_BASE
			                  ( IDT_IND, IDT_IND_BASE )
						   values
						      ( :IDT_IND, :IDT_IND_BASE )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_IND",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_IND_BASE",OracleType.Number)); 
				
                command.Parameters["IDT_IND"].Value = this.IDT_IND;
                command.Parameters["IDT_IND_BASE"].Value = this.IDT_IND_BASE;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Indicadores_base.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SRX_INDICADORES_BASE set 
			                   
						   where
						       IDT_IND = :IDT_IND and IDT_IND_BASE = :IDT_IND_BASE ";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_IND",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_IND_BASE",OracleType.Number)); 
				
                command.Parameters["IDT_IND"].Value = this.IDT_IND;
                command.Parameters["IDT_IND_BASE"].Value = this.IDT_IND_BASE;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Indicadores_base.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SRX_INDICADORES_BASE", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("IDT_IND", DbType.Int32, this.IDT_IND));
            tab.adicionarFiltro(new Coluna("IDT_IND_BASE", DbType.Int32, this.IDT_IND_BASE));
            tab.delete();
        }
		
		static public DataTable ObterTodos(string login)
        {
            
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_INDICADORES_BASE order by IDT_IND ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CIndicadoresBase PesquisaporCodigo(string idt_ind, string idt_ind_base, string login)
        {
            CIndicadoresBase obj;
            string SQL = @"SELECT *
                            FROM  Serix.SRX_INDICADORES_BASE 
                            WHERE IDT_IND = :IDT_IND and IDT_IND_BASE = :IDT_IND_BASE";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":IDT_IND", DbType.String, idt_ind);
				qy.AdicionarParametro(":IDT_IND_BASE", DbType.String, idt_ind_base);
               
				qy.Executar();
                obj = new CIndicadoresBase( qy.ObterCampo("IDT_IND"), qy.ObterCampo("IDT_IND_BASE") );
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CIndicadoresBase();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt(string login)
        {
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_IDT_INDIDT_IND_BASE() IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }

        static public DataTable ObterPorIDT_IND(string idt_ind, string login)
        {
            
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_INDICADORES_BASE  where IDT_IND = :IDT_IND order by IDT_IND ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_IND", OracleType.Number));
                command.Parameters["IDT_IND"].Value = idt_ind;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
        static public DataTable ObterPorIDT_IND_BASE(string idt_ind_base, string login)
        {
            
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_INDICADORES_BASE  where IDT_IND_BASE = :IDT_IND_BASE order by IDT_IND ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_IND_BASE", OracleType.Number));
                command.Parameters["IDT_IND_BASE"].Value = idt_ind_base;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
    }
}	

